package com.yida.controller;

import java.lang.ProcessBuilder.Redirect;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yida.service.serverphoto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yida.bean.phototype;
import com.yida.bean.photo;

@Controller
public class photos {
	
	@Autowired
	public serverphoto service;
	
	@RequestMapping("/photos")
	public String photo(Model mode,Integer id) {
		
		 phototype phototype = service.phototypebyid(id);	
		 if ( Integer.valueOf(phototype.getFlag()) == 1 ) {
			 mode.addAttribute("phototype", phototype);
		 	 List<photo> photos = service.photos(id);
			 mode.addAttribute("photos", photos);
			   return  "photo";
		}else {
			  return  "redirect:/phototype";
		}

	}
	
	//隐私相册
	@RequestMapping("/photos/expert")
	public String photoexpert(Model mode,Integer id,String password) {
		
    phototype phototype = service.phototypebyid(id);
  
       if (password.equals(phototype.getPassword())) {
    	   mode.addAttribute("phototype", phototype);
    	   List<photo> photos = service.photos(id);
    	   mode.addAttribute("photos", photos);
    	   return  "photo";
	   }else {
		   
		return  "redirect:/phototype";
	}
       
	
    
    
	}
	
	
	//验证密码
	@RequestMapping("/phototype/passwordverify")
	@ResponseBody
	public Object photo(Integer id,String password) throws JsonProcessingException {
		
		if (password!=null && id!=null) {
			phototype phototype = service.phototypebyid(id);
			String shujukupassword = phototype.getPassword();
			//System.out.println(shujukupassword);
			if (password == shujukupassword) {	
				Map<String, String> map = new HashMap<String, String>();
				map.put("result", "true");
				ObjectMapper objectMapper = new ObjectMapper();
				String  result =   objectMapper.writeValueAsString(map);  
				 return result;
			}else {
				Map<String, String> map = new HashMap<String, String>();
				map.put("result", "false");
				ObjectMapper objectMapper = new ObjectMapper();
				String  result =   objectMapper.writeValueAsString(map);  
				 return result;
			}
		}
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "false");
		ObjectMapper objectMapper = new ObjectMapper();
		String  result =   objectMapper.writeValueAsString(map);  
	    return result;
		
	}

}
